package com.yuezhu.receriver;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.igexin.sdk.PushConsts;
import com.yuezhu.global.MainActivity;
import com.yuezhu.R;
import com.yuezhu.util.LogUtils;

public class PushDemoReceiver extends BroadcastReceiver {
	private Context context = null;
	private String titleString;
	private String contentString;
	private static final String typeMedia = "01";
	private static final String typeOrg = "02";
	private static final String typeSys = "03";

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();
		this.context = context;
		Log.d("GetuiSdkDemo", "onReceive() action=" + bundle.getInt("action"));
		switch (bundle.getInt(PushConsts.CMD_ACTION)) {

		case PushConsts.GET_MSG_DATA:
			// 获取透传数据
			// String appid = bundle.getString("appid");
			byte[] payload = bundle.getByteArray("payload");

			/*
			 * String taskid = bundle.getString("taskid"); String messageid =
			 * bundle.getString("messageid");
			 */

			/*
			 * // smartPush第三方回执调用接口，actionid范围为90000-90999，可根据业务场景执行 boolean
			 * result = PushManager.getInstance().sendFeedbackMessage( context,
			 * taskid, messageid, 90001); System.out.println("第三方回执接口调用" +
			 * (result ? "成功" : "失败"));
			 */

			if (payload != null) {
				String data = new String(payload);
				shownotification(data);
			}
			break;
		}
	}

	// 推送通知栏设置
	public void shownotification(String msg) {
		LogUtils.e("推送通知", msg);
//		BusProvider.getInstance().register(this);
		// 通知生成
		NotificationManager barmanager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		int icon = R.drawable.ic_launcher;

		Notification.Builder builder = new Notification.Builder(context);
		builder.setSmallIcon(icon).setTicker("您有新的消息要查看");
		builder.setContentTitle("悦助").setContentText("hah");

			// 点击事件设置
			Intent appIntent = new Intent(Intent.ACTION_MAIN);
			appIntent.addCategory(Intent.CATEGORY_LAUNCHER);
			appIntent.setClass(context, MainActivity.class);
			appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
					| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

			Bundle bundle = new Bundle();
//			bundle.putSerializable(Intents.MSG_OBJECT, meBill);
			appIntent.putExtras(bundle);

	}

}
